#!/bin/bash

# Keep the current folder
curdir=`pwd`

# find the root dir
while [ ! -d ".git" ]; do
    cd ..
done

CODELITE_HOME=$(pwd)
echo "-- CODELITE_HOME is set to ${CODELITE_HOME}"

# Clear old installers
os_name=`uname -s`
arch=`uname -m`

os_name=`uname -s`

if [[ "$os_name" == "Darwin" ]]; then
    cpu_count=$(sysctl -n hw.ncpu)
    echo rm -f $curdir/build-release/codelite.app.tar.gz
    rm -f $curdir/build-release/codelite.app.tar.gz
elif [[ "$os_name" == "Linux" ]]; then
    cpu_count=$(nproc)
elif [[ "$os_name" = *"MSYS_NT"* ]]; then
    cpu_count=$(nproc)
fi

## parse the command line arguments and over defaults
BUILD_WXC="NO"
for i in "$@"
do
    case $i in
    --version=*)
        version="${i#*=}"
        shift
        ;;
    --with-wxc)
        BUILD_WXC="YES"
        shift
        ;;
    esac
done

if [ -z ${version} ]; then
    echo -e "*** ERROR ***: please provide version. e.g. --version=16.5.0"
    exit 1
fi

echo "-- Platform           : ${os_name}"
echo "-- CPU count          : ${cpu_count}"
echo "-- Version            : ${version}"
echo "-- Building wxCrafter : ${BUILD_WXC}"

# Update our source tree
cd ${CODELITE_HOME}
echo "-- Pulling CodeLite changes..."
git pull --rebase
if [ $? -ne 0 ]; then
    exit $?
fi

git pull --recurse-submodules
if [ $? -ne 0 ]; then
    exit $?
fi

cd $curdir
ORIG_PATH=$PATH
ORIG_LDPATH=$LD_LIBRARY_PATH

upload_path="/var/www/html/downloads/codelite/${version}"
